import { getListApi, updateChannelApi } from "@/api/configCenter/warehouseEntryChannel";
import { getOperationLogApi } from "@/api/common";
import { Modal, message } from "ant-design-vue";
import { createVNode } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
export default function OperationHook(state: Base.GeneraDataType) {
  const { searchForm, tableData, drawerOpts } = state;
  const getData = () => {
    const params = {
      ...searchForm,
      id: searchForm.id.length ? searchForm.id[2] : null
    };
    return getListApi(params).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        tableData.value = data.records || [];
        searchForm.total = Number(data.total);
      }
    });
  };
  const searchHandle = () => {
    searchForm.pageNum = 1;
    getData();
  };
  const addHandle = () => {
    drawerOpts.title = "新增渠道";
    drawerOpts.width = "1200px";
    drawerOpts.component = "AddChannel";
    drawerOpts.type = 1;
    drawerOpts.visible = true;
  };
  // 修改状态
  const changeStatusHandle = (row: CustomerManagement.ListRows) => {
    const params: any = {
      id: row.id
    };
    let tip = null;
    if (row.status === 1) {
      tip = "是否停用该渠道?";
      params.status = 0;
    } else {
      tip = "是否启用该渠道?";
      params.status = 1;
    }
    Modal.confirm({
      title: tip,
      icon: createVNode(ExclamationCircleOutlined),
      content: "",
      okText: "确定",
      okType: "danger",
      cancelText: "取消",
      onOk() {
        updateChannelApi(params).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            getData();
          }
        });
      }
    });
  };
  const editHandle = (row: WarehouseEntryChannel.ListRows) => {
    drawerOpts.title = "编辑渠道";
    drawerOpts.width = "1200px";
    drawerOpts.passData = row;
    drawerOpts.component = "AddChannel";
    drawerOpts.type = 2;
    drawerOpts.visible = true;
  };
  const editRateHandle = (row: WarehouseEntryChannel.ListRows) => {
    drawerOpts.title = "编辑报价";
    drawerOpts.width = "1200px";
    drawerOpts.passData = row;
    drawerOpts.component = "MakeOffers";
    drawerOpts.type = 1;
    drawerOpts.visible = true;
  };
  const showLogHandle = (row: WarehouseEntryChannel.ListRows) => {
    drawerOpts.title = "日志";
    drawerOpts.width = "1200px";
    drawerOpts.passData = row;
    drawerOpts.component = "OperationLog";
    drawerOpts.type = 2;
    drawerOpts.visible = true;
  };
  const requestFn = () => {
    return getOperationLogApi({ businessId: drawerOpts.passData?.id, sysModule: "WAREHOUSE_CHANNEL" });
  };
  return {
    getData,
    searchHandle,
    addHandle,
    changeStatusHandle,
    editHandle,
    editRateHandle,
    showLogHandle,
    requestFn
  };
}
